package com.jxwifi.cloud.quickcleanserver.mycenter;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicActivity;
import com.bumptech.glide.Glide;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.CleanBasicActivity;
import com.jxwifi.cloud.quickcleanserver.app.UrlPool;
import com.jxwifi.cloud.quickcleanserver.bean.InfoBean;
import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;
import com.jxwifi.cloud.quickcleanserver.presenter.CleaningSexPresenter;
import com.jxwifi.cloud.quickcleanserver.utils.PasswordLogin;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;
import com.jxwifi.cloud.quickcleanserver.view.CleaningSexView;


import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/5/21.
 */

public class CleaningSexActivity extends CleanBasicActivity implements CleaningSexView {

    @Bind(R.id.img_working_stop)
    ImageView mImgWorkingStop;
    @Bind(R.id.img_working_statue)
    ImageView mImgWorkingStatue;

    private boolean mIschecked;
    private String mWorkStatus;
    private CleaningSexPresenter mPresenter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cleaning_sex);
        ButterKnife.bind(this);
        openEventBus();
        initView();
    }

    public void initView(){
        if ("M".equals(AccountMgr.getsex(mBaseContext))){//工作中
            mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_ok);
            mImgWorkingStop.setBackgroundResource(R.mipmap.checked_un);
            mIschecked=true;
        }else if ("W".equals(AccountMgr.getsex(mBaseContext))){//休息中
            mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_un);
            mImgWorkingStop.setBackgroundResource(R.mipmap.checked_ok);
            mIschecked=false;
        }
        mPresenter=new CleaningSexPresenter(this);
    }

    @OnClick({R.id.rel_img_back_pressed,R.id.tv_working_confirm,R.id.rel_working_statue,R.id.rel_working_stop})
    void clickCK(View v){
        switch (v.getId()){
            case R.id.rel_img_back_pressed:
                onBackPressed();
                break;
            //工作状态
            case R.id.rel_working_statue:
                if (mIschecked){
                    mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_ok);
                    mImgWorkingStop.setBackgroundResource(R.mipmap.checked_un);
                    mIschecked=false;
                    mWorkStatus="M";
                }else{
                    mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_ok);
                    mImgWorkingStop.setBackgroundResource(R.mipmap.checked_un);
                    mIschecked=true;
                    mWorkStatus="M";
                }
                break;
            //休息状态
            case R.id.rel_working_stop:
                if (!mIschecked){
                    mImgWorkingStop.setBackgroundResource(R.mipmap.checked_ok);
                    mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_un);
                    mIschecked=true;
                    mWorkStatus="W";
                }else{
                    mImgWorkingStop.setBackgroundResource(R.mipmap.checked_ok);
                    mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_un);
                    mIschecked=false;
                    mWorkStatus="W";
                }
                break;
            case R.id.tv_working_confirm:
                //initdata();
                mPresenter.doinitdata();
                break;
        }
    }

    @Override
    public String getWorkStatus() {
        return mWorkStatus;
    }

    @Override
    public void onSuccess(int code, String data) {
        if (code==0){
            InfoBean mInfoBean= JSON.parseObject(data, InfoBean.class);
            AccountMgr.putUser(mBaseContext,mInfoBean,false);
            finish();
        }else{
            PasswordLogin.passwordlogin(mBaseContext,TAG,AccountMgr.gettell(mBaseContext),AccountMgr.getpassword(mBaseContext));
            if (PasswordLogin.mIsLogin){
                mPresenter.doinitdata();
            }
        }
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast =Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast,3000);
    }
}
